package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.Admin;
import com.hqyj.pojo.ApplicationRecord;
import com.hqyj.dao.ApplicationRecordMapper;
import com.hqyj.service.IApplicationRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-25
 */
@Service
public class ApplicationRecordServiceImpl extends ServiceImpl<ApplicationRecordMapper, ApplicationRecord> implements IApplicationRecordService {
//注入Mapper
    @Autowired(required = false)
    ApplicationRecordMapper applicationRecordMapper;
    @Override
    public HashMap<String, Object> select(ApplicationRecord a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<ApplicationRecord> page = new Page<>(a.getPage(),a.getRow());

        //条件构造器
        QueryWrapper<ApplicationRecord> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(a.getId()!=null,"id",a.getId());
        queryWrapper.like(a.getName()!=null&&!a.getName().equals(""),"name",a.getName());

        //查询数据库
        Page<ApplicationRecord> aList =applicationRecordMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",aList.getRecords());
        //总页数
        map.put("totalPage",aList.getPages());
        //总条数
        map.put("total",aList.getTotal());
        //当前页
        map.put("curPage",a.getPage());
        if(a.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",a.getPage()-1);

        }
        if(a.getPage()==aList.getPages()){
            //下一页
            map.put("nextPage",aList.getPages());

        }else{
            map.put("nextPage",a.getPage()+1);

        }
        //每页显示条数
        map.put("row",a.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> add(ApplicationRecord a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = applicationRecordMapper.insert(a);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(ApplicationRecord a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = applicationRecordMapper.updateById(a);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = applicationRecordMapper.deleteById(a);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }
}
